package com.movie2.controller;

import com.movie2.config.MyConstants;
import com.movie2.model.entity.*;
import com.movie2.model.request.TestRequest;
import com.movie2.model.vo.*;
import com.movie2.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/movie")
public class FilmController {
    @Autowired
    FilmService filmService;

    // 电影列表
    @GetMapping("")
    public List<Film> getAllFilms(@RequestParam(name="type",required = false,defaultValue = "全部")String type,
                                  @RequestParam(name="area",required = false,defaultValue = "全部")String area) {
        log.info("FilmController -> getAllFilms()");
        List<Film> films=null;
        // System.out.println(type);
        // System.out.println(area);
        if ("全部".equalsIgnoreCase(type) && "全部".equalsIgnoreCase(area)){
            films=filmService.selectAllFilms();
        }else if (!"全部".equalsIgnoreCase(type) && "全部".equalsIgnoreCase(area)){
            films=filmService.selectAllFilms(type,null);
        }else if ("全部".equalsIgnoreCase(type) && !"全部".equalsIgnoreCase(area)){
            films=filmService.selectAllFilms(null,area);
        }else {
            films=filmService.selectAllFilms(type,area);
        }
        return films;
    }

    // top榜单
    @GetMapping("/hot/limit")
    public List<Film> getAllFilmsHot() {
        log.info("FilmController -> getAllFilmsHot()");
        return filmService.getFilmsHot(10);
    }

    // @GetMapping("/movie_detail")
    // public Film getFilmById(@RequestParam("fid") int fid){
    //     log.info("FilmController -> getFilmById(@RequestParam(fid) int fid)");
    //     return filmService.selectByPrimaryKey(fid);
    // }

    // 电影详情
    @GetMapping("/detail")
    public Film getFilmById(@RequestParam("fid") int fid) {
        log.info("FilmController -> getFilmById(@RequestParam(fid) int fid)");
        Film film = filmService.selectByPrimaryKey(fid);
        film.setCover(MyConstants.MY_URL + film.getCover());
        return film;
    }

    @Autowired
    FilmReviewService filmReviewService;
    @Autowired
    UserService userService;

    // 影评
    @GetMapping("/review")
    public List<FilmReviewVo> getFilmReviewsByFid(@RequestParam("fid") int fid) {
        log.info("FilmController -> getFilmReviewsByFid(@RequestParam(fid) int fid)");
        List<FilmReviewVo> voList = new ArrayList<>();
        List<FilmReview> filmReviewList = filmReviewService.selectFilmReviewsByFid(fid);
        for (FilmReview filmReview : filmReviewList) {
            int uid = filmReview.getUid();
            FilmReviewVo map=new FilmReviewVo();
            User user = userService.selectByPrimaryKey(uid);
            map.setFilmReview(filmReview);
            user.setAvatar(MyConstants.MY_URL+user.getAvatar());
            map.setUser(user);
            voList.add(map);
        }
        // FilmReviewMapper里查询语句里加
        // 按时间倒序（最新发表的评论在最上面）
        // Collections.sort(voList, (o1, o2) -> {
        //     // SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //     Date dt1=o1.getFilmReview().getCreateAt();
        //     Date dt2=o2.getFilmReview().getCreateAt();
        //     if (dt1.getTime()>dt2.getTime()){
        //         return -1;
        //     }else if (dt1.getTime()<dt2.getTime()){
        //         return 1;
        //     }else {
        //         return 0;
        //     }
        // });
        return voList;
    }

    // 添加评论
    @PostMapping("/toreview")
    public Map<String,Object> addFilmReview(@RequestBody FilmReview filmReview){
        log.info("FilmController -> addFilmReview(@RequestBody FilmReview filmReview)");
        Map<String,Object> map=new HashMap<>();
        int count=filmReviewService.insert(filmReview);
        if (count==1){
            map.put("success","ok");
        }else {
            map.put("success","error");
        }
        return map;
    }

    // 名字模糊查询
    @GetMapping("/name")
    public List<Film> selectFilmsByCondition(@RequestParam("name")String name){
        log.info("FilmController -> selectFilmsByCondition(@RequestParam(name)String name)");
        System.out.println(name);
        Film f=new Film();
        f.setName(name);
        List<Film> films=filmService.selectFilmsByCondition(f);
        return films;
    }

    @Autowired
    ArrangementService arrangementService;

    // 根据电影查询排片
    @GetMapping("/arrangement/fid")
    public Map<String,Object> getArrangementByFid(@RequestParam("fid") Integer fid){
        log.info("FilmController -> getArrangementByFid(@RequestParam(\"fid\") Integer fid)");
        Map<String,Object> map=new HashMap<>();
        Arrangement temp=new Arrangement();
        temp.setFid(fid);
        temp.setDate(new Date());
        System.out.println(temp.getDate());
        List<Arrangement> arrangementList=arrangementService.selectArrangementsByCondition(temp);
        Film film=filmService.selectByPrimaryKey(fid);
        film.setCover(MyConstants.MY_URL + film.getCover());
        map.put("arrangements",arrangementList);
        map.put("film",film);
        return map;
    }

    // 根据排片id查询排片
    @GetMapping("/arrangement/aid")
    public Map<String,Object> getArrangementByAid(@RequestParam("aid") Integer aid){
        log.info("FilmController -> getArrangementByAid(@RequestParam(\"aid\") Integer aid)");
        Map<String,Object> map=new HashMap<>();
        Arrangement arrangement=arrangementService.selectByPrimaryKey(aid);
        Film film=filmService.selectByPrimaryKey(arrangement.getFid());
        film.setCover(MyConstants.MY_URL + film.getCover());
        map.put("arrangements",arrangement);
        map.put("film",film);
        return map;
    }

    @Autowired
    SeatService seatService;

    // 查询电影排片座位
    @GetMapping("/arrangement/getSeats")
    public List<SeatVo> getSeatsByFid(@RequestParam("aid") Integer aid){
        log.info("FilmController -> getSeatsByFid(@RequestParam(\"aid\") Integer aid)");
        List<SeatVo> voList = new ArrayList<>();
        List<Seat> seatList = seatService.selectByAid(aid);
        for (Seat seat : seatList) {
            int line=0;
            int row=0;
            if ((seat.getSeat()%12)==0){
                line=(int)(Math.floor(seat.getSeat()/12)-1);
            }else {
                line=(int)(Math.floor(seat.getSeat()/12));
            }
            if (seat.getSeat()%12==0){
                row=12;
            }else {
                row=seat.getSeat()%12;
            }
            SeatVo map=new SeatVo();
            // System.out.println(line);
            map.setLine(line);
            map.setRow(row);
            voList.add(map);
        }

        return voList;
    }
    

    // @GetMapping("/test4")
    // public List<Test1Vo> test4()  {
    //     return filmService.test4();
    // }
    // @GetMapping("/test5")
    // public List<Test2Vo> test5()  {
    //     return filmService.test5();
    // }
    // @PostMapping("/test6")
    // public void test6(@RequestBody List<Test2> test2List)  {
    //     filmService.test6(test2List);
    // }
    // @PostMapping("/test7")
    // public void test7(@RequestBody Test test)  {
    //     filmService.test7(test);
    // }
    // @PostMapping("/test8")
    // public void test8(@RequestBody TestRequest testRequest)  {
    //     filmService.test8(testRequest);
    // }
    // @PostMapping("/test9")
    // public void test9(@RequestBody List<Integer> list)  {
    //     filmService.test9(list);
    // }
    // @GetMapping("/test10")
    // public void test10(@RequestParam double num1,double num2)  {
    //     filmService.test10(num1,num2);
    // }
    // @PostMapping("/test11")
    // public void test11(@RequestBody List<Test> testList)  {
    //     filmService.test11(testList);
    // }
    // @GetMapping("/test12")
    // public void test12()  {
    //     filmService.test12();
    // }
    // @PostMapping("/test13")
    // public void test13(@RequestBody List<Long> testList)  {
    //     filmService.test13(testList);
    // }
    // @GetMapping("/test14")
    // public Test1_1Vo test14(@RequestParam List<Integer> idList)  {
    //     return filmService.test14(idList);
    // }







}
